<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Car_series_model extends CI_Model {

 
    function __construct()
    {
         parent::__construct();
    }
	function select_list_group_by_series()
	{
		 $this->db->from('base_carseries');
		 $this->db->order_by("brandid", "asc");
		 $this->db->order_by("seriesname", "asc"); 


		$query = $this->db->get();

		$result =$query->result_array();

		$group_result = array();
		foreach($result as $row)
		{
			$group_result[$row['brandid']][]=$row;
		}
		return $group_result;

	}
	function select_page($data = array(
			'seriesname'=>''  //系列名称
			,'brandid'=>''  //品牌id
			,'typeid'=>''  //类型id
			,'start'=>0		//开始记录数
			,'length'=>0))	//长度
	{
		
		if($data['seriesname'])
		{
			$this->db->like('seriesname',$data['seriesname']);
		}
		if($data['brandid'])
		{
			$this->db->where('brandid',$data['brandid']);
		}
		if($data['typeid'])
		{
			$this->db->where('typeid',$data['typeid']);
		}
	    $this->db->from('base_carseries');

		//查询总记录数
		$count = $this->db->count_all_results();

		$result['count'] = $count;
		/////////////////////////////////////////////////////////////////
		$this->db->select('base_carseries.*');
		$this->db->select('base_carbrand.brandname');
		$this->db->select('base_cartype.typename');
		if($data['seriesname'])
		{
			$this->db->like('seriesname',$data['seriesname']);
		}
		if($data['brandid'])
		{
			$this->db->where('base_carseries.brandid ',$data['brandid']);
		}
		if($data['typeid'])
		{
			$this->db->where('base_carseries.typeid ',$data['typeid']);
		}
	    $this->db->from('base_carseries');
		$this->db->join('base_carbrand', 'base_carseries.brandid = base_carbrand.brandid', 'left');
	    $this->db->join('base_cartype', 'base_carseries.typeid = base_cartype.typeid', 'left');

		if(intval($data['length'])<0)
		{
			$data['length'] =10;
		}
		if($data['start'])
		{
			$this->db->limit($data['length'],$data['start']);
		}
		else
		{
			$this->db->limit($data['length'],0);
		}
		$this->db->order_by("seriesname", "asc"); 


		$query = $this->db->get();

		$result['list']=$query->result_array();

		return $result;
	}


	function select_by_id($seriesid)
	{
		$this->db->where('seriesid',$seriesid);
		$query = $this->db->get('base_carseries');
		$row = $query->row_array();
		return $row;
	}

	function update($seriesid,$data=array())
	{
		$fields = array();
		if($data['seriesname'])
		{
			$fields['seriesname'] = $data['seriesname'];
		}
		if($data['brandid'])
		{
			$fields['brandid'] = $data['brandid'];
		}
		if($data['typeid'])
		{
			$fields['typeid'] = $data['typeid'];
		}
		if($data['seriesinfo'])
		{
			$fields['seriesinfo'] = $data['seriesinfo'];
		}
		if($data['seriesimage'])
		{
			$fields['seriesimage'] = $data['seriesimage'];
		}
		$this->db->where('seriesid', $seriesid);
		return $this->db->update('base_carseries', $fields); 
	}

	function insert($data=array())
	{
		$fields = array();
		if($data['seriesname'])
		{
			$fields['seriesname'] = $data['seriesname'];
		}
		if($data['brandid'])
		{
			$fields['brandid'] = $data['brandid'];
		}
		if($data['typeid'])
		{
			$fields['typeid'] = $data['typeid'];
		}
		if($data['seriesinfo'])
		{
			$fields['seriesinfo'] = $data['seriesinfo'];
		}
		if($data['seriesimage'])
		{
			$fields['seriesimage'] = $data['seriesimage'];
		}
	 
		$result = $this->db->insert('base_carseries', $fields); 

		return $result;
	}

	function delete($seriesid)
	{
		$result =array('success'=>true);
 
		$this->db->where('seriesid',$seriesid);

	    $this->db->from('base_carinfo');

		//查询总记录数
		$count = $this->db->count_all_results();

		if($count >0 )
		{
			$result['success']=false;
			$result['message'] ='删除车系信息失败，车系已在使用，请先删除对应的车辆信息！';
			return $result;
		}

		$this->db->where('seriesid',$seriesid);
		$result['data']  = $this->db->delete('base_carseries'); 
		return $result;
	}
}